package bravo.xc.com.xc_bravo_android;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import bravo.xc.com.xc_bravo_android.operation.login.LoginActivity;
import bravo.xc.com.xc_bravo_android.operation.person_center.PersonalCenterActivity;

public class SpashActivity extends AppCompatActivity {

    private final int REQUEST_WRITE_EXTERNAL_STORAGE = 0;
    private final int REQUEST_READ_PHONE_STATE = 1;
    private final int REQUEST_READ_EXTERNAL_STORAGE = 2;
    private final int REQUEST_ACCESS_FINE_LOCATION = 3;

    public Intent intent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(Build.VERSION.SDK_INT<Build.VERSION_CODES.M){
            judgePermission();
        }
        SharedPreferences sharedPreferences = getSharedPreferences("isFirstRun",MODE_PRIVATE);

        boolean isfirst = sharedPreferences.getBoolean("isFirstRun",true);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if(isfirst)
        {
            editor.putBoolean("isFirstRun",false);
            editor.commit();
            Intent intent = new Intent(SpashActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }else{
            checkSP();
        }
    }

    private void checkSP() {
        boolean isLogin = getSharedPreferences("isLogin",MODE_PRIVATE).getBoolean("flag",false);
        if(isLogin){
             intent = new Intent(SpashActivity.this, PersonalCenterActivity.class);
        }else {
             intent = new Intent(SpashActivity.this,LoginActivity.class);
        }
        startActivity(intent);
        finish();
    }

    private void judgePermission() {
        if(ContextCompat.checkSelfPermission(SpashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(SpashActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_WRITE_EXTERNAL_STORAGE);
        }
        if(ContextCompat.checkSelfPermission(SpashActivity.this, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(SpashActivity.this,new String[]{Manifest.permission.READ_PHONE_STATE},REQUEST_READ_PHONE_STATE);
        }
        if(ContextCompat.checkSelfPermission(SpashActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(SpashActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_READ_EXTERNAL_STORAGE);
        }
        if(ContextCompat.checkSelfPermission(SpashActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(SpashActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_ACCESS_FINE_LOCATION);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case REQUEST_ACCESS_FINE_LOCATION:
                if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(SpashActivity.this,"位置权限请求成功",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(SpashActivity.this,"请到设置中给予应用位置权限",Toast.LENGTH_SHORT).show();
                }
                break;
            case REQUEST_READ_EXTERNAL_STORAGE:
                if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(SpashActivity.this,"文件权限请求成功",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(SpashActivity.this,"请到设置中给予应用位文件权限",Toast.LENGTH_SHORT).show();
                }
                break;
            case REQUEST_READ_PHONE_STATE:
                if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(SpashActivity.this,"信息获取权限请求成功",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(SpashActivity.this,"请到设置中给予应用信息获取权限",Toast.LENGTH_SHORT).show();
                }
                break;
            case REQUEST_WRITE_EXTERNAL_STORAGE:
                if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(SpashActivity.this,"文件读写权限请求成功",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(SpashActivity.this,"请到设置中给予应用文件读写权限",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
